package com.souku.finance.application.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Android on 2017/1/3.
 */

public class EdittextListenUtil {

    public static void calculation(final EditText editText1, final EditText editText2, final EditText editText3, final double point) {

        editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = editText1.getText().toString().trim();
                String s2 = editText2.getText().toString().trim();
                if (!MyTextUtil.isEmpty(s1) && !MyTextUtil.isEmpty(s2)) {
                    editText3.setText(Sum(s1, s2, point));
                } else {
                    editText3.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = editText1.getText().toString().trim();
                String s2 = editText2.getText().toString().trim();
                if (!MyTextUtil.isEmpty(editText1.getText().toString().trim()) && !MyTextUtil.isEmpty(editText2.getText().toString().trim())) {
                    editText3.setText(Sum(s1, s2, point));
                } else {
                    editText3.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private static String Sum(String ext1, String ext2, double point) {
        return MyTextUtil.doubleFormat(Integer.parseInt(ext1) * Integer.parseInt(ext2) * point / 36500);
    }

    public static void calculation2(final EditText editText1, final EditText editText2, final TextView editText3, final double point) {

        editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = editText1.getText().toString().trim();
                String s2 = editText2.getText().toString().trim();
                if (!MyTextUtil.isEmpty(s1) && !MyTextUtil.isEmpty(s2)) {
                    editText3.setText(Sum(s1, s2, point));
                } else {
                    editText3.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1 = editText1.getText().toString().trim();
                String s2 = editText2.getText().toString().trim();
                if (!MyTextUtil.isEmpty(editText1.getText().toString().trim()) && !MyTextUtil.isEmpty(editText2.getText().toString().trim())) {
                    editText3.setText(Sum(s1, s2, point));
                } else {
                    editText3.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public static void setDouble(final EditText et_wysg, final double max) {
        et_wysg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (str.equals(".")) {
                    et_wysg.setText("");
                }
                if (str.contains(".")) {
                    if (str.length() - str.indexOf(".") == 4) {
                        et_wysg.setText(str.substring(0, str.length() - 1));
                        et_wysg.setSelection(str.length() - 1);
                    }
                }
                if (!(str.isEmpty() | str.equals("."))) {
                    if (max != 0) {
                        if (Double.parseDouble(str) > max) {
                            et_wysg.setText(MyTextUtil.doubleFormat(max));
                            et_wysg.setSelection(et_wysg.getText().toString().length());
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}
